package com.hs.carrental.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hs.carrental.dto.admin.AdminPageDto;
import com.hs.carrental.dto.admin.AdminSaveDto;
import com.hs.carrental.entity.Admin;
import com.hs.carrental.mapper.AdminMapper;
import com.hs.carrental.service.IAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hs.carrental.util.Helper;
import com.hs.carrental.util.PasswordEncrypter;
import com.hs.carrental.vo.admin.AdminPageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lyy
 * @since 2021-03-17
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Autowired
    private AdminMapper adminMapper;

    /**
     * 编辑或新增
     * @param adminSaveDto
     * @return
     * @throws UnsupportedEncodingException
     */
    @Override
    public boolean save(AdminSaveDto adminSaveDto) throws UnsupportedEncodingException {
        Admin admin = new Admin();
        //密码加密
        if (!Helper.isEmpty(adminSaveDto.getAdminPassword())){
            adminSaveDto.setAdminPassword(PasswordEncrypter.getEncryptedPwd(adminSaveDto.getAdminPassword()));
        }
        //复制属性
        BeanUtils.copyProperties(adminSaveDto,admin);
        //编辑或新增 无主键新增 有主键更新
        boolean saveOrUpdate = this.saveOrUpdate(admin);
        return saveOrUpdate;
    }

    /**
     * 分页查询
     * @param adminPageDto
     * @return
     */
    @Override
    public IPage<AdminPageVo> getPage(AdminPageDto adminPageDto) {
        //分页对象
        Page<Admin> page = new Page<>(adminPageDto.getPageNum(),adminPageDto.getPageSize());
        //分页查询
        IPage<AdminPageVo> pageVos = adminMapper.getPage(page, adminPageDto.getAdminName(), adminPageDto.getStoreId());
        return pageVos;
    }
}
